package com.kkb.shopcenter.server.impl;

import com.kkb.shopcenter.api.ProductInfoService;
import com.kkb.shopcenter.common.pojo.GoodsInfo;
import com.kkb.shopcenter.common.pojo.ProductInfo;
import com.kkb.shopcenter.common.pojo.ProductIntroduce;
import com.kkb.shopcenter.common.pojo.ProductIntroduceWithBLOBs;
import com.kkb.shopcenter.server.mappers.GoodsInfoMapper;
import com.kkb.shopcenter.server.mappers.ProductInfoMapper;
import com.kkb.shopcenter.server.mappers.ProductIntroduceMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class ProductInfoServiceImpl implements ProductInfoService {

    @Autowired
    private ProductInfoMapper  productInfoMapper;

    @Autowired
    private GoodsInfoMapper goodsInfoMapper;

    @Autowired
    private ProductIntroduceMapper productIntroduceMapper;

    @Override
    @Transactional
    public int addProductInfo(ProductInfo productInfo, String extrAttibute, String introduce) {
        //添加产品
        productInfoMapper.insertUseGeneratedKeys(productInfo);
        //返回产品ID
        Long productId = productInfo.getId();
        //获取产品对列表应商品列表
        List<GoodsInfo> goodList = productInfo.getGoodsList();
        //计算商品数量
        int result = 0;
        for(GoodsInfo goodsInfo:goodList){
            goodsInfo.setProductId(productId);
            goodsInfo.setShopId(productInfo.getShopId());
            goodsInfo.setStatus((byte)1);
            //添加商品对象
            goodsInfoMapper.insertSelective(goodsInfo);
            result++;
        }

        ProductIntroduce productIntroduce = new ProductIntroduce();

        productIntroduce.setProductId(productId);

        ProductIntroduceWithBLOBs productIntroduceWithBLOBs = new ProductIntroduceWithBLOBs();
        productIntroduceWithBLOBs.setExtrattribute(extrAttibute);
        productIntroduceWithBLOBs.setIntroduce(introduce);
        //添加产品介绍对象
        productIntroduceMapper.insertSelective(productIntroduce);

        return result;
    }
}
